package com.zhouyu.controller;

import com.zhouyu.domain.User;
import com.zhouyu.service.StringToUserEditor;
import com.zhouyu.service.ZhouyuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;

@RestController
public class ZhouyuController {

	@Autowired
	private ZhouyuService zhouyuService;

	@GetMapping("/test")
	public String test() {
		return zhouyuService.test();
	}

	@GetMapping("/test2")
	public String test2(String requestparam) {
		System.out.println("requestParam:" + requestparam);
		return "success";
	}

	@GetMapping("/test3")
	public String test3(@RequestParam(value = "requestparam", required = false) String requestParam) {
		System.out.println("requestParam:" + requestParam);
		return "success";
	}

	@GetMapping("/test4")
	public String test4(@RequestHeader(value = "requestheader", required = false) String requestHeader) {
		System.out.println("requestHeader:" + requestHeader);
		return "success";
	}

	@PostMapping("/test5")
	public String test5(@RequestBody(required = false) String requestBody) {
		System.out.println("requestBody:" + requestBody);
		return "success";
	}

	@PostMapping("/test6")
	public String test6(@CookieValue(value = "cookievalue", required = false) String cookieValue) {
		System.out.println("cookieValue:" + cookieValue);
		return "success";
	}

	@PostMapping("/test7")
	public String test7(String param,
						@RequestParam(value = "requestparam", required = false) String requestParam,
						@RequestBody(required = false) String requestBody,
						@RequestPart(value = "requestpart", required = false) String requestPart,
						@RequestHeader(value = "requestheader", required = false) String requestHeader,
						@RequestAttribute(value = "requestattribute", required = false) String requestAttribute,
						@SessionAttribute(value = "sessionattribute", required = false) String sessionAttribute,
						@CookieValue(value = "cookievalue", required = false) String cookieValue) {
		System.out.println("param:" + param);
		System.out.println("requestParam:" + requestParam);
		System.out.println("requestBody:" + requestBody);
		System.out.println("requestPart:" + requestPart);
		System.out.println("requestHeader:" + requestHeader);
		System.out.println("requestAttribute:" + requestAttribute);
		System.out.println("sessionAttribute:" + sessionAttribute);
		System.out.println("cookieValue:" + cookieValue);
		return "success";
	}

	@GetMapping("/test8")
	@ResponseBody
	public String test8() {
		return "测试";
	}

	@GetMapping(value = "/test9", produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String test9() {
		return "测试";
	}

	@GetMapping("/test10")
	@ResponseBody
	public byte[] test10() {
		return "测试".getBytes(StandardCharsets.UTF_8);
	}

	@GetMapping("/user1")
	public String user1(User user) {
		return user.toString();
	}

	@PostMapping("/user2")
	public String user2(User user) {
		return user.toString();
	}

	@PostMapping("/user3")
	public String user3(@RequestBody User user) {
		return user.toString();
	}

	@GetMapping("/user4")
	public String user4(@RequestParam("name") User user) {
		return user.toString();
	}

	@GetMapping("/user5")
	public String user5(@RequestParam User user) {
		return user.toString();
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(User.class, new StringToUserEditor());
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file")
	@ResponseBody
	public String file(MultipartFile file) {
		if (file != null) {
			System.out.println(file.getOriginalFilename());
		}
		return "success";
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file2")
	@ResponseBody
	public String file2(MultipartFile file, MultipartFile file2) {
		if (file != null) {
			System.out.println(file.getOriginalFilename());
		}
		if (file2 != null) {
			System.out.println(file2.getOriginalFilename());
		}
		return "success";
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file3")
	@ResponseBody
	public String file3(MultipartFile file, String test) {
		if (file != null) {
			System.out.println(file.getOriginalFilename());
		}
		System.out.println(test);
		return "success";
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file4")
	@ResponseBody
	public String file4(MultipartFile file, @RequestPart String test) {
		if (file != null) {
			System.out.println(file.getOriginalFilename());
		}
		System.out.println(test);
		return "success";
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file5")
	@ResponseBody
	public String file5(@RequestPart String test) {
		System.out.println(test);
		return "success";
	}

	@RequestMapping(method = RequestMethod.POST, path = "/file6")
	@ResponseBody
	public String file6(String test) {
		System.out.println(test);
		return "success";
	}
}
